<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!-- <meta name="viewport" content="user-scalable=no,viewport-fit=cover" /> -->
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <link href="./css/baseCss.css" rel="stylesheet" type="text/css"/>
    <link href="./base/layer.css" rel="stylesheet" type="text/css"/>
    <!-- <link href="./base/articlecon.css" rel="stylesheet" type="text/css"/> -->
    <link href="./base/base2.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="./base/rem.js"></script>
    <script type="text/javascript" src="./base/iscroll.js"></script>
    <style type="text/css">
        section {
            background-color: #fff;
            margin-top: 0.25rem;
            padding: 0 0.5rem;
        }

        .list {
            position: relative;
            border-bottom: 0.02rem solid #f5f5f5;
            min-height: 1.67rem;
        }

        .list_title {
            display: inline-block;
            width: 2.15rem;
            margin-top: 0.5rem;
        }

        .address_book {
            font-size: 0.45rem;
            color: #0089ff;
            position: absolute;
            right: 0;
            top: 0.5rem;
        }

        .list_consignee {
            display: inline-block;
            border-bottom: 0.02rem solid #f5f5f5;
            width: 76%;
            padding-bottom: 0.15rem;
        }

        .list_size_inc {
            display: inline-block;
            margin-bottom: 0.4rem;
        }

        .list_size_inc_consignee {
            margin-left: 2.3rem;
            margin-top: 0.4rem;
        }

        .list_size_inc span {
            display: inline-block;
            border: 0.03rem solid #eeeeee;
            font-size: 0.3rem;
            border-radius: 0.1rem;
            vertical-align: top;
            margin-top: 0.05rem;
            width: 1.25rem;
            text-align: center;
            color: #666666;
            height: 0.6rem;
            line-height: 0.6rem;
            margin-right: 0.34rem;
            box-shadow: 0rem 0rem 0.2rem -0.1rem;
        }

        span.list_size_target {
            border-color: #ff6160;
            color: #ff6160;
            background-color: #fbeded;
        }

        .redBtn {
            width: 90%;
            margin: 0 auto;
            display: block;
            margin-top: 1.34rem;
        }
    </style>
    <title>新增地址</title>
</head>

<body>
<div class="main">
    <header class="allHeader">
        <div class="headerReturn" onclick="window.location.href='./address.html'"><img class="imgBai"
                                                                                       src="./img/nav_bake.png"></div>
        <span class="addOreditor">收货地址</span>
        <div class="headerList"><img class="imgBai" src="./img/nav_menu.png"></div>
    </header>
    <section>
        <div class="list">
            <div class="list_title">联系人</div>
            <div class="list_consignee">
                <input type="text" class="consignee" placeholder="姓名">
            </div>

            <div id="sex" class="list_size_inc list_size_inc_consignee"><span
                    class="list_size_target">先生</span><span>女士</span></div>
        </div>
        <div class="list">
            <div class="list_title">电话</div>
            <input type="text" class="phone" placeholder="请输入联系电话">
            <!-- <div class="address_book">+通讯录</div> -->
        </div>
        <div id="address" class="list">
            <div class="list_title">地址</div>
            <input type="text" class="address" placeholder="请选择收货地址" readonly="true" onclick="disSave();">
        </div>
        <div class="list">
            <div class="list_title">门牌号</div>
            <input type="text" class="houseNumber" placeholder="例：14A栋1701室">
        </div>
        <div>
            <div class="list_title">标签</div>
            <div id="tag" class="list_size_inc"><span class="list_size_target">家</span><span>公司</span><span>学校</span>
            </div>
        </div>
    </section>
    <footer>
        <button class="redBtn">确定</button>
    </footer>
</div>

</body>
</html>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/httpUnit.js"></script>
<script type="text/javascript" src="./js/constants.js"></script>
<script type="text/javascript">
    selectSexTag();//点击选择性别和标签
    isupOradd();

    //修改或添加完成
    function success(index){
        if(index==0){
            alert("添加成功");
        }else{
            alert("修改成功");
        }
        window.location.href='./address.html';
    }

    //判断本页面是新增还是修改页面
    function isupOradd() {
        var Request = new Object();
        Request = GetRequest();
        var addressId = Request['addressId'];
        var phone = Request['phone'];
        var consignee = decodeURI(Request['consignee']);
        var sex = Request['sex'];
        var tag = Request['tag'];
        var houseNumber = decodeURI(Request['houseNumber']);

        var longitude = Request['longitude'];
        var latitude = Request['latitude'];

        if (phone == undefined || consignee == undefined) {

        } else {
            $(".phone").val(phone);
            $(".consignee").val(consignee);
            $(".houseNumber").val(houseNumber);

            sex == 0 ? $("#sex span").eq(0).addClass("list_size_target").siblings().removeClass("list_size_target") : $("#sex span").eq(1).addClass("list_size_target").siblings().removeClass("list_size_target");
            if (tag == 1) {
                $("#tag span").eq(0).addClass("list_size_target").siblings().removeClass("list_size_target");
            } else if (tag == 2) {
                $("#tag span").eq(1).addClass("list_size_target").siblings().removeClass("list_size_target");
            } else {
                $("#tag span").eq(2).addClass("list_size_target").siblings().removeClass("list_size_target");
            }
        }

        if (addressId == undefined) {
            addFull();
        } else {
            updateFull(addressId, longitude, latitude);
        }
    }


    //修改方法填充所有数据
    function updateFull(addressId, longitude, latitude) {
        var userId = "1";

        $(".address").val(localStorage.getItem("address"));

        $("footer").html("<button class='redBtn update' onclick=success(1);>修改</button>");
    }

    //填充省市区及经纬度
    function addFull() {
        var userId = "1";
        var lat = localStorage.getItem("lat");
        var lng = localStorage.getItem("lng");
        var province = localStorage.getItem("province");
        var city = localStorage.getItem("city");
        var district = localStorage.getItem("district");
        var address = localStorage.getItem("address");

        var html = "";
        if (lat != null) {
            // html = "<div class='selectaddress' onclick='disSave();'>"
            //         + "<div class='province'>"+province+"</div>"
            //         +"<div class='city'>"+city+"</div>"
            //         +"<div class='district'>"+district+"</div>"
            //         +"<div class='address'>"+address+"</div>";
            //         +"</div>";
            $("#address input").val(address);
        } else {
            // html = "<div class='selectaddress' onclick='disSave();'>请选择地址</div>";
        }

        // $(".addAddress").html(html);
        // $(".isupOradd").html("<div class='add'>添加</div>");
        $("footer").html("<button class='redBtn' onclick=success(0);>确定</button>");
        // onclick=addUserAddress("+userId+","+province+","+city+","+district+","+address+","+lng+","+lat+");
        // $(".redBtn").click(function(){
        //      addUserAddress(userId,province,city,district,address,lng,lat);
        // });

    }

    //添加地址逻辑
    function addUserAddress(userId, province, city, district, address, longitude, latitude) {
        var phone = $(".phone").val();
        var consignee = $(".consignee").val();
        var houseNumber = $(".houseNumber").val();
        var tag = 0;
        if ($("#tag .list_size_target").text() == "家") {
            tag = 1;
        } else if ($("#tag .list_size_target").text() == "公司") {
            tag = 2;
        } else {
            tag = 3;
        }
        var sex = 0;
        $("#sex .list_size_target").text() == "先生" ? sex = 0 : sex = 1;

        var obj = JSON.stringify({
            "userId": userId,
            "province": province,
            "city": city,
            "district": district,
            "address": address,
            "longitude": longitude,
            "latitude": latitude,
            "phone": phone,
            "consignee": consignee,
            "houseNumber": houseNumber,
            "tag": tag,
            "sex": sex,
        });

        if (phone == "") {
            alert("请输入手机或电话号码");
        } else if (phone.match(/^1\d{10}$/) == null && phone.match(/^0\d{2,3}-?\d{7,8}$/) == null) {
            alert("请输入正确的手机或电话号码格式");
        } else if (consignee == "") {
            alert("请输入联系人");
        } else if (province == null) {
            alert("请输入地址");
        } else {
            httpHelperpost(_API_ADD_USER_ADDRESS, obj, function (isSuccess, data) {
                if (isSuccess == true) {
                    var response = data.responseText;
                    var value = JSON.parse(response);

                    console.log(response);


                    if (value.code == 0) {
                        console.log("添加成功");
                        window.location.href = "./address.html";
                    } else if (value.code == 401) {
                        alert(value.message);
                        window.location.href = './index.html';
                    } else {
                        alert(value.message);
                        console.log("添加失败");
                    }
                } else {
                }
            });
        }
    }

    //修改用户地址逻辑
    function updateUserAddress(id, userId, longitude, latitude) {

        var province = $(".province").text();
        var city = $(".city").text();
        var district = $(".district").text();
        var address = $(".address").val();
        var phone = $(".phone").val();
        var consignee = $(".consignee").val();
        var houseNumber = $(".houseNumber").val();
        var sex = 0;
        var tag = 0;
        $("#sex .list_size_target").text() == "先生" ? sex = 0 : sex = 1;
        if ($("#tag .list_size_target").text() == "家") {
            tag = 1;
        } else if ($("#tag .list_size_target").text() == "公司") {
            tag = 2;
        } else {
            tag = 3;
        }

        var obj = JSON.stringify({
            "id": id,
            "userId": userId,
            "province": province,
            "city": city,
            "district": district,
            "address": address,
            "longitude": longitude,
            "latitude": latitude,
            "phone": phone,
            "consignee": consignee,
            "houseNumber": houseNumber,
            "sex": sex,
            "tag": tag,
        });

        if (phone == "") {
            alert("请输入手机或电话号码");
        } else if (phone.match(/^1\d{10}$/) == null && phone.match(/^0\d{2,3}-?\d{7,8}$/) == null) {
            alert("请输入正确的手机或电话号码格式");
        } else if (consignee == "") {
            alert("请输入联系人");
        } else if (province == null) {
            alert("请输入地址");
        } else {
            httpHelperpost(_API_UPDATE_USER_ADDRESS, obj, function (isSuccess, data) {
                if (isSuccess == true) {
                    var response = data.responseText;
                    var value = JSON.parse(response);

                    console.log(response);


                    if (value.code == 0) {
                        console.log("修改成功");
                        window.location.href = "./address.html";
                    } else if (value.code == 401) {
                        alert(value.message);
                        window.location.href = './index.html';
                    } else {
                        alert(value.message);
                        console.log("修改失败");
                    }
                } else {
                }
            });
        }
    }

    //点击跳转到选择位置界面并传递保存数据
    function disSave() {
        var Request = new Object();
        Request = GetRequest();
        var addressId = Request['addressId'];

        var phone = $(".phone").val();
        var consignee = $(".consignee").val();

        var sex = 0;
        $("#sex .list_size_target").text() == "先生" ? sex = 0 : sex = 1;
        var tag = 0;
        if ($("#tag .list_size_target").text() == "家") {
            tag = 1;
        } else if ($("#tag .list_size_target").text() == "公司") {
            tag = 2;
        } else {
            tag = 3;
        }
        var houseNumber = $(".houseNumber").val();

        if (addressId == undefined) {
            window.location.href = "./position.html?phone=" + phone + "&consignee=" + consignee + "&sex=" + sex + "&tag=" + tag + "&houseNumber=" + houseNumber;
        } else {

            window.location.href = "./position.html?phone=" + phone + "&consignee=" + consignee + "&addressId=" + addressId + "&sex=" + sex + "&tag=" + tag + "&houseNumber=" + houseNumber;
        }

    }

    //点击选择性别和标签
    function selectSexTag() {
        $(".list_size_inc span").click(function () {
            $(this).addClass("list_size_target").siblings().removeClass("list_size_target");
        });
    }

    //获取url中"?"符后的字串
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串   

        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);

            strs = str.split("&");

            for (var i = 0; i < strs.length; i++) {

                theRequest[strs[i].split("=")[0]] = strs[i]
                    .split("=")[1];
            }
        }
        return theRequest;
    }
</script>

